<link rel="stylesheet" href="<?php echo Yii::app()->request->baseUrl; ?>/css/jquery.tooltip.css" />
<script type="text/javascript" src="<?php echo Yii::app()->request->baseUrl; ?>/js/jquery.bgiframe.js"></script>
<script type="text/javascript" src="<?php echo Yii::app()->request->baseUrl; ?>/js/jquery.dimensions.js"></script>
<script type="text/javascript" src="<?php echo Yii::app()->request->baseUrl; ?>/js/jquery.tooltip.js"></script>

<script>
/*
* update the datepicker's date value with the endID
* to the datepicker's date value with the beginID
*/
function CascadeChange(beginID, endID)
{
	var element1=document.getElementById(beginID);
	var element2=document.getElementById(endID);
	var date1=new Date(element1.value);
	var date2=new Date(element2.value);
	if(date1.getTime()>=date2.getTime())
	{
		//update the date2's date exactly with the date1's date
		date2.setTime(date1.getTime()+0*24*60*60*1000+60000*date1.getTimezoneOffset());
	}
	element2.value=date2.format('yyyy-MM-dd');
}

/*
* time formating function
*/
Date.prototype.format = function(format){
 /*
  * eg:format="YYYY-MM-dd hh:mm:ss";
  */
 var o = {
  "M+" :  this.getMonth()+1,  //month
  "d+" :  this.getDate(),     //day
  "h+" :  this.getHours(),    //hour
      "m+" :  this.getMinutes(),  //minute
      "s+" :  this.getSeconds(), //second
      "q+" :  Math.floor((this.getMonth()+3)/3),  //quarter
      "S"  :  this.getMilliseconds() //millisecond
   }
  
   if(/(y+)/.test(format)) {
    format = format.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
   }
 
   for(var k in o) {
    if(new RegExp("("+ k +")").test(format)) {
      format = format.replace(RegExp.$1, RegExp.$1.length==1 ? o[k] : ("00"+ o[k]).substr((""+ o[k]).length));
    }
   }
 return format;
}

/*
* update other field's info (site, products....)
* send ajax request to retrieve site details
* after changing the selected tank 
*/
function updateTankInfo(data)
{
	var prefix='<?php echo get_class($auction_model)?>';
	var suffix=new Array("_product_ID", "_product_name", "_site_ID", "_site_name", "_capacity", "_inventory_last_read", "_inventory_last_time");
	for(i=0;i<suffix.length;i++)
	{
		var element=document.getElementById(prefix+suffix[i]);
		if(i<data.length)
		{
			element.value=data[i];
		}
		else
		{
			element.value='';
		}
	}
	
	var siteID=document.getElementById(prefix+'_site_ID').value;
	var tankID=document.getElementById(prefix+'_tank_ID').value;
	if(siteID!=null&&siteID>0)
	{
		$.ajax({
	       url: '<?php echo CController::createUrl('auctions/siteDetails');?>',
	       type: 'post',
	       dataType: 'json',
	       data: {site_ID:siteID, tank_ID:tankID},
	       success: updateSiteDetails
	     });
	}
	else
	{
		$("#site-details-button").tooltip();
	}
}

/*
* enable necessary fields when submitting initiate auction request  
*/
function enableFields()
{
	var prefix='<?php echo get_class($auction_model)?>';
	var suffix=new Array("_product_ID", "_product_name", "_site_ID", "_site_name", "_capacity", "_inventory_last_read", "_inventory_last_time");
	for(i=0;i<suffix.length;i++)
	{
		var element=document.getElementById(prefix+suffix[i]);
		element.disabled=false;
	}
}

/*
* create a new tooltip,
* display the site details info in the tooltip,
* after changing the selected auction
*/
function updateSiteDetails(data)
{
	var result='';
	
	var tank=data[0];
	var site=data[1];
	var rack=data[2];
	var tools=data[3];
	var tankType=data[4];
	var tankPosition=data[5];
	var tankMonitor=data[6];
	
	result+='<b>SITE ID:</b> '+site.site_ID+'</br>';
	result+='<b>SITE NAME:</b> '+site.site_name+'</br>';
	result+='<b>Address 1:</b> '+site.address1+'</br>';
	result+='<b>Address 2:</b> '+site.address2+'</br>';
	result+='<b>City/State/Zip:</b> '+site.city+', '+site.state+', '+site.zip+'</br>';
	result+='<b>County:</b> '+site.county+'</br>';
	result+='<b>Home Rack:</b> '+rack.city+', '+rack.state+'</br>';
	if(site.gated==1)
	{
		result+='<b>Gated:</b> YES</br>';
		result+='<b>Gated Hours:</b> '+site.gate_opens+'-'+site.gate_closes+'</br>';
	}
	else
	{
		result+='<b>Gated:</b> NO</br>';
	}
	if(site.security_on_premises==1)
	{
		result+='<b>Hour of Operations:</b> '+site.open_time+'-'+site.close_time+'</br>';
	}
	for(var i=0;i<tools.length;i++)
	{
		result+='<b>'+tools[i].tool_name+':</b> YES</br>';
	}
	result+='</br>';
	
	result+='<b>TANK ID:</b> '+tank.tank_ID+'</br>';
	result+='<b>TANK NAME:</b> '+tank.tank_name+'</br>';
	result+='<b>Type of Tank:</b> '+tankType.tank_type_description+'</br>';
	result+='<b>Orientation:</b> '+tankPosition.tank_position_description+'</br>';
	if(tankMonitor!=null)
	{
		result+='<b>Tank Monitor MFG:</b> '+tankMonitor.monitor_MFG+'</br>';
		result+='<b>Tank Monitor Model:</b> '+tankMonitor.monitor_model+'</br>';
	}
	if(tank.remote_fill==1)
	{
		result+='<b>Remote Fill:</b> YES</br>';
	}
	if(tank.overfill_alarm==1)
	{
		result+='<b>Overfill Alarm:</b> YES</br>';
	}
	result+='</br>';
	
	result+='<b>Contact Name:</b> '+site.contact_name+'</br>';
	result+='<b>Phone# of Site:</b> '+site.phone_number_of_site+'</br>';
	result+='<b>After Hour #:</b> '+site.after_hour_number+'</br>';
	
	$("#site-details-button").tooltip({
    	bodyHandler: function() {
	     return result;
	   },
	   showURL: false
    });
}

</script>
<?php
$this->layout='main';
date_default_timezone_set('America/New_York');
?>

<?php
//create a time list, the time every 30mins in one day is inserted 
function timeList()
{
	$timelist=array();
	for($i=0;$i<48;$i++)
	{
		$time=date('H:i', strtotime('today '.'+'.(30*$i).'minutes'));
		$timelist[$time]=date('h:i a', strtotime('today '.'+'.(30*$i).'minutes'));
	}
	return $timelist;
}
?>

<?php $form = $this->beginWidget('CActiveForm', array(
		'id'=>'iniauction-form',
	//	'enableClientValidation'=>true,
	//	'enableAjaxValidation'=>true,
		));
?>

<div id="lets-initiate">Lets Initiate</div>

<div id="initiate-auction-container">
<div id="initiate-contents">

<div id="initiate-left">

<div class="initiate-top">
    <div id="initiate-head"><img src="<?php echo Yii::app()->request->baseUrl; ?>/images/initiate-head.gif" alt="" /></div>
    <div id="step1"><img src="<?php echo Yii::app()->request->baseUrl; ?>/images/step1.gif" alt="" /></div>
</div>

<div id="initiate-box1">

<div align='middle' style='color:#FF0000; padding-top: 10px'>
<?php
	echo $form->error($auction_model, 'purchase_order_number');
	echo $form->error($auction_model, 'gallon');
	echo $form->error($auction_model, 'auction_start');
	echo $form->error($auction_model, 'tank_ID');
	echo $form->error($auction_model, 'auction_end');
	echo $form->error($auction_model, 'delivery_due');
	echo $form->error($auction_model, 'notes');
?>
</div>

<div id="initiate-details">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <th>P.O. #</th>
    <td>
	    <div class="textbox3">
			<?php echo $form->textField($auction_model, 'purchase_order_number'); ?>
			<!-- echo $form->hiddenField($auction_model, 'PO_Num'); -->
    	</div>
    </td>
    <th width="75">Site Name</th>
    <td>
    	<div class="textbox3">
			<?php echo $form->textField($auction_model, 'site_name', array('disabled'=>true)); ?>
			<?php echo $form->hiddenField($auction_model, 'site_ID'); ?>
    	</div>
    </td>
  </tr>
  <tr>
    <th width="115">Tank #</th>
    <td>
    <div class="textbox4">
		<?php echo $form->dropDownList($auction_model, 'tank_ID', 
							Tanks::model()->loadTankNames(),
							array(
								'style'=>"width:212px;",
								'ajax'=>array(
									'type'=>'POST',
									'url'=>CController::createUrl('iniAuction/tankinfo'),
									'success'=>"updateTankInfo",
									'dataType'=>'json',
								),
							)
						); ?>
    </div>
    </td>
    <th>Tank Capacity</th>
    <td>
    	<div class="textbox3">
    		<?php echo $form->textField($auction_model, 'capacity', array('disabled'=>true)); ?>
    	</div>
    </td>
  </tr>
  <tr>
    <th>Products</th>
    <td>
	    <div class="textbox3">
			<?php echo $form->textField($auction_model, 'product_name', array('disabled'=>true)); ?>
			<?php echo $form->hiddenField($auction_model, 'product_ID'); ?>
    	</div>
    </td>
    <th>Gallons</th>
    <td>
    	<div class="textbox3">
    		<?php echo $form->textField($auction_model, 'gallon'); ?>
    	</div>
    </td>
  </tr>
  <tr>
    <th>Inventory<br />Last Read</th>
    <td>
    	<div class="textbox3">
    		<?php echo $form->textField($auction_model, 'inventory_last_read', array('disabled'=>true)); ?>
    	</div>
    </td>
    <th>Invertory<br />Last Time</th>
    <td>
    	<div class="textbox3">
    		<?php echo $form->textField($auction_model, 'inventory_last_time', array('disabled'=>true)); ?>
    	</div>
    </td>
  </tr>
  <tr>
    <th>Auction <br />Begin Date</th>
    <td>
    	<div class="textbox3">
    		<?php
				$this->widget('zii.widgets.jui.CJuiDatePicker',
					array(
						'name'=>'begindate',
						'value'=>date('Y-m-d'),
						'options'=>array(
							'dateFormat'=>'yy-mm-dd',
							'changeMonth'=>'true',
							'changeYear'=>'true',
							'constrainInput'=>'true',
							'showAnim'=>'slide',
							'minDate'=>''.date('Y-m-d'),
						),
						'htmlOptions'=>array(
							'onchange'=>'CascadeChange(this.id,\'enddate\');CascadeChange(this.id,\'duedate\');'
						)
					));
			?>	
    	</div>
    </td>
    <th>Auction <br/>End Date</th>
    <td>
    	<div class="textbox3">
    		<?php
				$this->widget('zii.widgets.jui.CJuiDatePicker',
					array(
						'name'=>'enddate',
						'value'=>date('Y-m-d'),
						'options'=>array(
							'dateFormat'=>'yy-mm-dd',
							'changeMonth'=>'true',
							'changeYear'=>'true',
							'constrainInput'=>'true',
							'showAnim'=>'slide',
							'minDate'=>''.date('Y-m-d'),
						)
					));
			?>
    	</div>
    </td>
  </tr>
  <tr>
    <th>Auction<br/>Start Time</th>
    <td>
    	<div class="textbox3">
			<?php 
			/*	$this->widget('zii.widgets.jui.CJuiAutoComplete', array(
					'name'=>'starttime',
					'value'=>'',
					'source'=>timeList(),
					'options'=>array(
								'		showAnim'=>'fold',
									),
				));*/
				echo CHtml::dropDownList('starttime', '0:00', timeList(), array('style'=>'width:212px'));
			?>
    	</div>
    </td>
    <th>Auction<br/>End Time</th>
    <td>
    	<div class="textbox3">
    		<?php 
			/*	$this->widget('zii.widgets.jui.CJuiAutoComplete', array(
					'name'=>'endtime',
					'value'=>'',
					'source'=>timeList(),
					'options'=>array(
								'		showAnim'=>'fold',
									),
				));*/
				echo CHtml::dropDownList('endtime', '0:00', timeList(), array('style'=>'width:212px'));
			?>
    	</div>
    </td>
  </tr>
  <tr>
    <th>Delivery<br/>Due Date</th>
    <td>
    	<div class="textbox3">
    		<?php
				$this->widget('zii.widgets.jui.CJuiDatePicker',
					array(
						'name'=>'duedate',
						'value'=>date('Y-m-d'),
						'options'=>array(
							'dateFormat'=>'yy-mm-dd',
							'changeMonth'=>'true',
							'changeYear'=>'true',
							'constrainInput'=>'true',
							'showAnim'=>'slide',
							'minDate'=>''.date('Y-m-d'),
						)
					));
			?>
    	</div>
    </td>
    <th>Delivery<br/>Due Time</th>
    <td>
    	<div class="textbox3">
    		<?php 
			/*	$this->widget('zii.widgets.jui.CJuiAutoComplete', array(
					'name'=>'duetime',
					'value'=>'',
					'source'=>timeList(),
					'options'=>array(
								'		showAnim'=>'fold',
									),
				));*/
				echo CHtml::dropDownList('duetime', '0:00', timeList(), array('style'=>'width:212px'));
			?>
    	</div>
    </td>
  </tr>
</table>
</div><!-- End Initiate Details -->

<ul class="green-buttons">
    <li><a id='site-details-button' href="#"><img src="<?php echo Yii::app()->request->baseUrl; ?>/images/site-details.gif" alt="" /></a></li>
</ul><!-- End Green Buttons -->

<div id="notes">
    <div id="notes-head">Notes</div>
    <div id="notes-text">
    	<?php echo $form->textArea($auction_model, "notes", array('cols'=>'', 'rows'=>'')); ?>
    </div>
</div><!-- End Notes -->

</div><!-- End Initiate Box1 -->
</div><!-- End Initiate Left -->

<div id="initiate-bottom">
    <div id="initiate-button">
    	<input type="image" src="<?php echo Yii::app()->request->baseUrl; ?>/images/initiate2.gif" onclick='enableFields()'/>
    </div>
</div><!-- End Initiate Bottom -->

</div><!-- End Initiate Contents -->
</div><!-- End Initiate Auction Container -->


<?php $this->endWidget(); ?>